const express = require("express");
const router = express.Router();
const jwt = require("../utils/jwtHelp");
router.post("/login", (req, res, next) => {
  const { account, password } = req.body;
  const { session, sessionID } = req;
  console.log(sessionID, session.user);
  if (!session.user) {
    session.user = {
      account,
      password,
    };
    res.status(200).json({
      code: 200,
      msg: "登录成功",
      data: jwt.signed(sessionID),
    });
  } else {
    const flag =
      session.user.account === account && session.user.password === password;
    if (flag) {
      res.status(200).json({
        code: 200,
        msg: "登录成功",
        data: jwt.signed(sessionID),
      });
    } else {
      res.status(401).json({
        code: 401,
        msg: "登录失败",
        data: null,
      });
    }
  }
});

module.exports = router;
